#!/bin/bash
workDir=pwd
echo "当前的工作目录为"${workDir}

env_content=`cat ~/.bash_profile | grep -i java`
echo "环境变量为${env_content}"
if ! [ -n "$env_content" ]; then
    echo "jdk is set and not empty,无需重复安装jdk"
    return
fi

fileName=`ls jdk1.8*.tar.gz`
if ! [ -n "$fileName" ]; then
  echo "文件不存在,请先上传jdk安装包"
  return
fi
echo "文件名为${fileName}"
tar -xzvf ./$fileName
jdkUnzipFileName=`ls |grep jdk |grep -v ".tar.gz"`

echo "jdk解压后的文件名为"${jdkUnzipFileName}
# 添加环境变量
echo "PATH=$PATH:$HOME/bin" >> ~/.bash_profile
echo "export JAVA_HOME=/usr/local/${jdkUnzipFileName}" >> ~/.bash_profile
echo "export CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar" >> ~/.bash_profile
echo "export PATH=$PATH:$JAVA_HOME/bin" >> ~/.bash_profile
echo "export MAVEN_HOME=/usr/local/apache-maven-3.3.9" >> ~/.bash_profile

# 刷新环境变量
source ~/.bash_profile
# 输出信息
java --version